package com.tech.cloudapp.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.tech.cloudapp.model.CommonResponseDO;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 描述:
 * 创建者:wangkang
 * 邮箱: victorwangk@qq.com
 * smart-open-cloud
 * 日期:2019/12/28
 */
@Slf4j
@Configuration
@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@RestControllerAdvice
public class GobalException {

    @ExceptionHandler(value = BusinessException.class)
    public Object errorExceptionHandler(HttpServletRequest request, HttpServletResponse response, BusinessException e) throws Exception{
        log.error("业务异常", e);
        return JSON.toJSONString(CommonResponseDO.fail("500",e.getMessage()),new PascalNameFilter());
    }

    @ExceptionHandler(value = Exception.class)
    public Object errorExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) throws Exception{

        log.error("系统异常",e);
        return JSON.toJSONString(CommonResponseDO.fail("500","服务器执行异常"),new PascalNameFilter());
    }


}
